﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Networking;
using UnityEngine;


public delegate void AssetBundleLoadProgress(string assetBundleName,float progress);
public delegate void AssetBundleComplete(string assetBundleName);
public delegate void LoadAssetBundleCallBack(string sceneName,string assetBundleName);
public class AssetBundleLoader
{
    private AssetLoader assetLoader;
    private UnityWebRequest request;

    /// <summary>
    /// 资源加载进度
    /// </summary>
    private AssetBundleLoadProgress onProgress;
    /// <summary>
    /// 资源加载完成
    /// </summary>
    private AssetBundleComplete onComplete;
    /// <summary>
    /// AssetBundle资源名称
    /// </summary>
    public string assetBundleName;
    /// <summary>
    /// AssetBundle资源路径
    /// </summary>
    private string assetBundlePath;

    public AssetBundleLoader(AssetBundleComplete onComplete,AssetBundleLoadProgress onProgress, string assetBundleName)
    {
        this.onComplete = onComplete;
        this.onProgress = onProgress;
        this.assetBundleName = assetBundleName;
        this.assetBundlePath = PathUnit.GetWWWPath()+"/"+ assetBundleName;
        assetLoader = null;
        request = null;
    }
    public IEnumerator Load()
    {
        yield return 0;
        AssetBundle assetBundle= AssetBundle.LoadFromFile(assetBundlePath);
        assetLoader = new AssetLoader(assetBundle);
        onComplete?.Invoke(assetBundleName);
        
    }
    public AssetBundle Asset { get { return assetLoader.assetBundle; } }
    public UnityEngine.Object LoadAsset(string assetName)
    {
        if (assetLoader == null )
        {
            Debug.LogError("当前assetLoader is null");
            return null;
        }
        return assetLoader.LoadAsset(assetName);
    }
    public T LoadAsset<T>(string assetName) where T : UnityEngine.Object
    {
        if (assetLoader == null)
        {
            Debug.LogError("当前assetLoader is null");
            return null;
        }
        return assetLoader.LoadAsset<T>(assetName);
    }

    public T[] LoadAssetAll<T>() where T : UnityEngine.Object
    {
        return assetLoader?.LoadAssetAll<T>();
    }
    /// <summary>
    /// 获取图集资源的全部子集
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="assetName"></param>
    /// <returns></returns>
    public UnityEngine.Object[] LoadAssetWithSubAssets(string assetName)
    {
        return assetLoader?.LoadAssetWithSubAssets(assetName);
    }
    /// <summary>
    /// 卸载资源
    /// </summary>
    /// <param name="asset"></param>
    public void UnLoadAsset(UnityEngine.Object asset)
    {
        assetLoader?.UnLoadAsset(asset);
    }
    /// <summary>
    /// 释放assetbundle资源
    /// </summary>
    public void Dispose()
    {
        assetLoader?.Dispose();
        assetLoader = null;
    }
    /// <summary>
    /// 测试专用，获取当前assetbundle资源包的已经加载好的全部资源名称
    /// </summary>
    /// <returns></returns>
    public string[] GetAllAssetNames()
    {
        return assetLoader?.GetAllAssetNames();
    }
    public void Trace()
    {
        assetLoader?.Trace();
    }
}